package com.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.dao.BaseDAO;
import com.entity.Stu;
import com.entity.Userinfo;
import com.entity.Yegui;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class YeguiAction extends ActionSupport {
	Yegui data;
	Stu stu;
	BaseDAO dao = new BaseDAO();

	public String add() {
		//new StuAction().add();
		ActionContext.getContext().put("all", dao.show("from Stu where status=2"));
//		Userinfo users = (Userinfo) ActionContext.getContext().getSession().get("loginUser");
//		String role = users.getUserType();
//		if ("学生".equals(role)) {
//			ActionContext.getContext().put("all",
//					dao.find("from Stu where stuno=?", new Object[] { users.getLoginName() }));
//			return "show";
//		} else {
//			ActionContext.getContext().put("all", dao.show("from Stu"));
			return "show";
//		}
		// return "show";
	}

	public String del() {
		dao.del(dao.findByID(Yegui.class, data.getId()));
		return add();
	}
	public String log() {
		ActionContext.getContext().put("all", dao.show("from Yegui"));
		return SUCCESS;
	}
	public String in() {
		Stu stu = (Stu) dao.findByID(Stu.class, data.getId());
		stu.setStatus(1);
		
		Yegui info = new Yegui();
		info.setId(stu.getId());
		info.setName(stu.getName());
		info.setDepartment(stu.getDepartment());
		info.setRoomno(stu.getRoomno());
		info.setCurtime(new Date());
		Yegui date = (Yegui)dao.findByID(Yegui.class, stu.getId());
		//System.out.println(date.getOutTime());
		info.setOutTime(date.getOutTime());
		dao.update(info);
		dao.update(stu);

		ActionContext.getContext().put("all", dao.show("from Yegui"));
		return SUCCESS;
	}
	
	public String find() {
		String hql = "from Stu where 1=1";
		List params = new ArrayList();
		
		
		if(!"".equals(data.getStuno())){
			hql+=" and stuno=?";
			params.add(data.getStuno());
		}
		if(!"".equals(data.getArea())){
			hql+=" and area=?";
			params.add(data.getArea());
		}
		if(!"".equals(data.getFlat())){
			hql+=" and flat=?";
			params.add(data.getFlat());
		}
		if(!"".equals(data.getRoomno())){
			hql+=" and roomno=?";
			params.add(data.getRoomno());
		}
		ActionContext.getContext().put("all", dao.find(hql, params.toArray()));
		return "school";
	}
	public String find1() {
		String hql = "from Stu where 1=1";
		List params = new ArrayList();
		
		
		if(!"".equals(data.getStuno())){
			hql+=" and stuno=?";
			params.add(data.getStuno());
		}
		
		ActionContext.getContext().put("all", dao.find(hql, params.toArray()));
		return "out";
	}
	
	
	public String find2() {
		String hql = "from Stu where 1=1";
		List params = new ArrayList();
		
		if(!"".equals(data.getCurtime())){
			hql+=" and curtime=?";
			params.add(data.getCurtime());
		}
		
		ActionContext.getContext().put("all", dao.find(hql, params.toArray()));
		return "in";
	}

	public Yegui getData() {
		return data;
	}

	public void setData(Yegui data) {
		this.data = data;
	}
}
